#include "head.h"

char log_name[128] = {};
char log_passwd[128] = {};


int do_login(int sockfd, MSG *pbuf)
{
	MSG msg,r_buf;
	enum USER_LEVEL level;
	int buf,i;
	memset(&msg,0,sizeof(msg));
	char i_name[128] = {};
	char i_pass[128] = {};

	while(1)
	{
start:	printf("==============client============\n");
		printf("请输入登录名字：\n");
		gets(i_name);
		strcpy(msg.name,i_name);
		printf("请输入登录密码：\n");
		gets(i_pass);
		strcpy(msg.passwd,i_pass);

		send(sockfd, &msg, sizeof(MSG), 0);
		
		memset(&r_buf,0,sizeof(MSG));
		i =	recv(sockfd, &r_buf, sizeof(MSG), 0);
		if(!strncmp(r_buf.data,"SUCCESS",7))
		 {
			level =  r_buf.type;
			strcpy(log_name,r_buf.name);
			strcpy(log_passwd,r_buf.passwd);
			return level;
		}else 
			goto start;

	}

}



//修改密码
void staff_change_passwd(int sockfd, MSG *pbuf)
{
	
	int sockFd = sockfd;
	MSG msg = *pbuf;
	
	strcpy(msg.info.name, log_name);
	printf("please input new password:");
	scanf("%s", msg.passwd);

	msg.type = STAFF_CHANGE_PASSWD;

	send(sockFd, &msg, sizeof(msg), 0);
}


//发送增加用户命令
void addUser(int sockfd, MSG *pbuf)
{
	MSG msg = *pbuf;
	int sockFd = sockfd;
	char buf[100] = {};
	printf("input id:");
	scanf("%d", &msg.info.id);

	printf("input name:");
	scanf("%s", msg.info.name);

	printf("input passwd:");
	scanf("%s", msg.passwd);

	printf("input age:");
	scanf("%d", &msg.info.age);

	printf("input addr:");
	scanf("%s", msg.info.addr);

	printf("input salary:");
	scanf("%f", &msg.info.salary);

	msg.type = ADMIN_ADD_STAFF;


	send(sockFd, &msg, sizeof(msg), 0);
//	strcpy(msg.data,"");
	memset(buf,0,100);
	recv(sockFd,buf, sizeof(buf), 0);
	printf("recv from server: %s\n", buf);
}
//发送删除用户命令
void deleteUser(int sockfd, MSG *pbuf)
{
	
	int sockFd = sockfd;
	MSG msg = *pbuf;
	printf("input name:");
	scanf("%s", msg.info.name);
	
	msg.type = ADMIN_DEL_STAFF;

	char buf[100];
	send(sockFd, &msg, sizeof(msg), 0);
	memset(buf, 0, sizeof(buf));
	recv(sockFd, buf, sizeof(buf), 0);
	printf("recv from server: %s\n", buf);

}

//发送普通查询用户信息命令

void do_staff_query(int sockfd, MSG *pbuf)
{
	int sockFd = sockfd;
	MSG msg = *pbuf;
	
	strcpy(msg.info.name, log_name);

	msg.type = STAFF_QUERY;

	char buf[100];
	send(sockFd, &msg, sizeof(msg), 0);
	memset(buf, 0, sizeof(buf));
	recv(sockFd, &buf, sizeof(buf), 0);
	printf("%s", buf);
}
//发送管理员查询用户信息命令

void do_admin_query(int sockfd, MSG *pbuf)
{
	int sockFd = sockfd;
	MSG msg = *pbuf;

	printf("please input name:");
	scanf("%s", msg.info.name);

	msg.type = STAFF_QUERY;

	char buf[100];
	send(sockFd, &msg, sizeof(msg), 0);
	memset(buf, 0, sizeof(buf));
	recv(sockFd, &buf, sizeof(buf), 0);
	printf("%s", buf);
}


//普通用户界面
void do_staff(int sockfd, MSG *pbuf)
{
	int sockFd = sockfd;
	MSG msg = *pbuf;
	while(1)
	{
		printf("*******************staff********************************\n");
   		printf("*******1: show user 2: change password 3: quit**********\n");
		printf("********************************************************\n");
		printf("please select:\n");

		int n;
		char clean[64];
		if (scanf("%d", &n) != 1)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		} 

		switch ( n )
		{
		case 1 :
			do_staff_query(sockfd, &msg);
			break;
		case 2 :
			staff_change_passwd(sockFd, &msg);
			break;
		case 3:
			return;
		default:
			printf("err option\n");
			break;
		}
	}
	
}
//管理员界面
int do_admin(int sockfd, MSG *pbuf)
{

	int sockFd = sockfd;
	MSG msg = *pbuf;
	while(1)
	{
		printf("***********************admin**************************************\n");    
		printf("***1: add user 2:delete user 3: query (name)   4.quit***\n");
		printf("******************************************************************\n");
		printf("please select : ");

		int n;
		char clean[64];
		if (scanf("%d", &n) != 1)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		} 

		switch ( n )
		{
		case 1 :
			addUser(sockFd, &msg);
			break;
		case 2 :
			deleteUser(sockFd, &msg);
			break;
		case 3 :
			do_admin_query(sockFd, &msg);
			break;
		case 4 :
			do_admin_query_all(sockFd,&msg);
		default:
			printf("err option\n");
			break;
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }

    ret = do_login(sockfd, &buf);
    if(ret == STAFF){  //员工
        puts("staff login");
        do_staff(sockfd, &buf);
    }else if(ret == ADMIN){  //管理员
        puts("admin login");
        do_admin(sockfd, &buf);
    }
}
